package com.example.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.ResultCode;
import com.example.dao.*;
import com.example.entity.Dianying;
import com.example.exception.CustomException;
import com.example.service.CollectService;
import com.example.service.DianyingService;
import com.example.utils.UserBasedCollaborativeFiltering;
import com.example.vo.DianyingVo;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

@Service("dianyingService")
public class DianyingServiceImpl extends ServiceImpl<DianyingDao, Dianying> implements DianyingService {

    @Resource
    private DianyingDao dianyingDao;

    @Lazy
    @Resource
    private CollectService collectService;

    public Dianying add(Dianying dianying) {
		
	   //youquchong1
        dianying.setAddtime(new Date());
        dianyingDao.insert(dianying);
        return dianying;
    }
	
	
	
    public void delete(Long id) {
        dianyingDao.deleteById(id);
    }
	

    public void update(Dianying dianying) {
        //youquchong2
        dianyingDao.updateById(dianying);
    }
	
	@Override
    public List<Map<String, Object>> daochuexcel() {
        return dianyingDao.daochuexcel();
    }

    @Override
    public Dianying findById(Integer id) {
        return dianyingDao.selectById(id);
    }

    public DianyingVo findPage(DianyingVo dianyingVo) {
        LambdaQueryWrapper<Dianying> wrapper = Wrappers.lambdaQuery();
		wrapper.like(ObjectUtils.isNotEmpty(dianyingVo.getDianyingbianhao()), Dianying::getDianyingbianhao,dianyingVo.getDianyingbianhao());
        wrapper.like(ObjectUtils.isNotEmpty(dianyingVo.getDianyingmingcheng()), Dianying::getDianyingmingcheng,dianyingVo.getDianyingmingcheng());
        wrapper.like(ObjectUtils.isNotEmpty(dianyingVo.getDianyingleixing()), Dianying::getDianyingleixing,dianyingVo.getDianyingleixing());
        wrapper.like(ObjectUtils.isNotEmpty(dianyingVo.getDaoyan()), Dianying::getDaoyan,dianyingVo.getDaoyan());
        wrapper.like(ObjectUtils.isNotEmpty(dianyingVo.getZhuyan()), Dianying::getZhuyan,dianyingVo.getZhuyan());
        wrapper.like(ObjectUtils.isNotEmpty(dianyingVo.getJuqingjieshao()), Dianying::getJuqingjieshao,dianyingVo.getJuqingjieshao());
        wrapper.like(ObjectUtils.isNotEmpty(dianyingVo.getFaxingriqi()), Dianying::getFaxingriqi,dianyingVo.getFaxingriqi());
        wrapper.like(ObjectUtils.isNotEmpty(dianyingVo.getPiaofang()), Dianying::getPiaofang,dianyingVo.getPiaofang());
        wrapper.like(ObjectUtils.isNotEmpty(dianyingVo.getPingfen()), Dianying::getPingfen,dianyingVo.getPingfen());
        wrapper.like(ObjectUtils.isNotEmpty(dianyingVo.getTupian()), Dianying::getTupian,dianyingVo.getTupian());
        wrapper.like(ObjectUtils.isNotEmpty(dianyingVo.getBeizhu()), Dianying::getBeizhu,dianyingVo.getBeizhu());
        wrapper.like(ObjectUtils.isNotEmpty(dianyingVo.getId()), Dianying::getId,dianyingVo.getId());
        wrapper.orderByDesc(Dianying::getPingfen);
        
        Page<Dianying> page = new Page<>(dianyingVo.getCurrent(),dianyingVo.getCurrentNum());
        dianyingDao.selectPage(page,wrapper);
        List<Dianying> dianyings = page.getRecords();
        dianyingVo.setList(dianyings);
        dianyingVo.setPages(page.getPages());
        dianyingVo.setTotal(page.getTotal());
        dianyingVo.setCurrent(page.getCurrent());
        dianyingVo.setCurrentNum(page.getSize());
        return dianyingVo;
    }

    @Override
    public void deleteList(List<Dianying> list) {
        List<Integer> ids =  list.stream().map(Dianying::getId).collect(Collectors.toList());
        dianyingDao.deleteBatchIds(ids);
    }


    @Override
    public Map<String, Double> recommend(Integer userId, Integer num) {
        // 创建用户-物品收藏矩阵
        Map<String, Set<String>> userItemCollections = new HashMap<>();

        List<Map<String, String>> maps = collectService.getCollected();

        for (Map<String, String> map : maps) {
            userItemCollections.put(String.valueOf(map.get("user_id")), new HashSet<>(Arrays.asList(map.get("items").split(","))));
        }

        UserBasedCollaborativeFiltering cf = new UserBasedCollaborativeFiltering(userItemCollections);

        // 为目标用户推荐物品
        Map<String, Double> recommendations = cf.recommendItems(String.valueOf(userId), num);

        // 输出推荐结果
        System.err.println("基于用户收藏的推荐 用户ID:" + userId);
        for (Map.Entry<String, Double> entry : recommendations.entrySet()) {
            System.err.println("商品ID: " + entry.getKey() + ", 分数: " + entry.getValue());
        }

        return recommendations;
    }

    @Override
    public List<Dianying> findByIds(List<Integer> ids) {
        return dianyingDao.selectBatchIds(ids);
    }

}
